package com.frogs.ucenter.service.daoimpl.aop;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.component.permission.api.service.PermissionHelper;
import com.frogs.component.permission.entity.User;
import com.frogs.system.util.ChangeTypeEnum;
import com.frogs.ucenter.dao.TaskInfoDao;
import com.frogs.ucenter.dao.TaskInfoLogDao;
import com.frogs.ucenter.entity.TaskInfo;
import com.frogs.ucenter.entity.TaskInfoLog;

@Aspect
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service("taskInfoLogAopImpl")
public class TaskInfoLogAopImpl {
	@Resource
	private TaskInfoLogDao taskInfoLogDao;
	@Resource
	private TaskInfoDao taskInfoDao;
	@Resource(name = "permissionHelper")
	private PermissionHelper permissionHelper;
	
	private static final Logger log = LoggerFactory.getLogger(TaskInfoLogAopImpl.class);

	
	@AfterReturning(pointcut="execution(* com.frogs.ucenter.controller.TaskInfoController.saveTaskInfo(..)) || "
			+ "execution(* com.frogs.ucenter.controller.TaskInfoController.updateTaskInfo(..)) || "
			+ "execution(* com.frogs.ucenter.controller.TaskInfoController.deleteTaskInfo(..))")
	public void doAfterReturning(JoinPoint jp)
	{
		try {
			HttpServletRequest req = (HttpServletRequest)jp.getArgs()[0];//切入点方法入参
			boolean flag = (Boolean) req.getAttribute("flag");
			String method = jp.getStaticPart().getSignature().getName();
			if(!flag){
				return;
			}
			TaskInfo taskInfo = new TaskInfo();
			int changeType = 0;
			if(method.equals("saveTaskInfo")){
				taskInfo = (TaskInfo)jp.getArgs()[3];
				changeType = ChangeTypeEnum.ADD.getId();
			}else if(method.equals("updateTaskInfo")){
				taskInfo = (TaskInfo)jp.getArgs()[3];
				changeType = ChangeTypeEnum.UPDATE.getId();
			}else if(method.equals("deleteTaskInfo")){
				String deleteId = jp.getArgs()[3].toString();
				String[] arr = deleteId.split(",");
				taskInfo.setId(Integer.parseInt(arr[0]));
				changeType = ChangeTypeEnum.DELETE.getId();
			}else{
				return;
			}
			User user = permissionHelper.getUser();
			insertLog(taskInfo, changeType, user.getUserId());
		} catch (Exception e) {
			log.error("func[doAfterReturning] jp[" + jp + "] error[" + e.getMessage() + "]", e);
		}
		
	}
	
	@Async
	private void insertLog(TaskInfo taskInfo, int changeType, int creater){
		TaskInfoLog taskInfoLog = new TaskInfoLog();
		taskInfoLog.setActionType(taskInfo.getActionType());
		taskInfoLog.setApplicablePlatformId(taskInfo.getApplicablePlatformId());
		taskInfoLog.setChangeType(changeType);
		taskInfoLog.setCompleteMoney(taskInfo.getCompleteMoney());
		taskInfoLog.setCompleteNum(taskInfo.getCompleteNum());
		taskInfoLog.setCreater(creater);
		taskInfoLog.setCycle(taskInfo.getCycle());
		taskInfoLog.setEndTime(taskInfo.getEndTime());
		taskInfoLog.setExpandCode(taskInfo.getExpandCode());
		taskInfoLog.setGiveCardId(taskInfo.getGiveCardId());
		taskInfoLog.setGiveCardName(taskInfo.getGiveCardName());
		taskInfoLog.setGiveMoney(taskInfo.getGiveMoney());
		taskInfoLog.setGivePoint(taskInfo.getGivePoint());
		taskInfoLog.setLevelId(taskInfo.getLevelId());
		taskInfoLog.setNumberUpperLimit(taskInfo.getNumberUpperLimit());
		taskInfoLog.setOldId(taskInfo.getId());
		taskInfoLog.setRemark(taskInfo.getRemark());
		taskInfoLog.setStartTime(taskInfo.getStartTime());
		taskInfoLog.setStatus(taskInfo.getStatus());
		taskInfoLog.setTaskType(taskInfo.getTaskType());
		taskInfoLog.setTaskUrl(taskInfo.getTaskUrl());
		taskInfoLog.setUrl(taskInfo.getUrl());
		taskInfoLogDao.insert(taskInfoLog);
	}
}
